<?php
// +----------------------------------------------------------------------
// | 青航系统 [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2023-永久 https://www.feifeiyu.club/ All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 青航-不吃芒果 <1716892803@qq.com>
// +----------------------------------------------------------------------
namespace app\admin\controller;
use think\exception\Jump;
use think\facade\View;
use app\admin\validate\Banner_validate;
class Banner extends  Common{
    
    
    
    // 数据列表
    public function index() {
        $list = D('banner')->order('id DESC')->paginate(15);
    	return view('banner/index',['list'=>$list]);
    }
    
    
    
     // 添加数据
    public function add() {
       if (request()->isPost()) {
            // 获取全部参数
    	    $postField = 'title,image,url,typeid,introduce,status,listorder';
			$data = request()->only(explode(',',$postField),'post',null);
    	     // 加载验证器
    	    $validate = new Banner_validate();
    	    
            if (!$validate->check($data)) {
               Jump::error($validate->getError());
            }
            
            $data['inputtime'] = time();
            $r = D('banner')->insert($data);
           
    		// 插入数据
    		if($r){
    		    Jump::success("添加成功!");
    		}
            else {
                Jump::error('添加错误!!');
            }
    		
    	}
    	sleep(1);
    	return view('banner/banner_add');
    }
    
    
    
    
    
     // 更新数据
    public function edit() {
        // 获取网页中提交的ID
    	$id = request()->param('id');
    	// 	查询对应字段的参数
    	$find = D('banner')->where('id',$id)->find();
    	if (request()->isPost()) {
    	     // 获取全部参数
    	    $postField = 'id,title,image,url,typeid,introduce,status,listorder';
			$data = request()->only(explode(',',$postField),'post',null);
    	     // 加载验证器
    	    $validate = new Banner_validate();
    	    
            if (!$validate->check($data)) {
               Jump::error($validate->getError());
            }
            
            $data['inputtime'] = time();
            $r = D('banner')->where('id', $data['id'])->update($data);
            
    		if($r){
    		   Jump::success("更新成功!"); 
    		}
    		else {
    		    Jump::error('数据未改动!!');
    		}
            
    	}
    	sleep(1);
    	return view('banner/banner_edit',['find'=>$find]);
    }
    
    
   
    
    // 删除数据
    public function del() {
    	if (request()->isPost()) {
    	   // 获取当前网页的参数ID
    		$id = request()->param('id');
    		if(empty($id) ) {Jump::error('ID不存在!');}
            if($data = D('banner')->where('id',$id)->delete()){
    		  Jump::success("删除成功!!");     
    		}
    		else{
    		  Jump::error('删除失败!');
    		}
    		
    	}
    }
    
    

    
}